<?php

namespace Utility;

use Zend\Mvc\MvcEvent;
use Zend\Session\SessionManager;
use Zend\Session\Container;

use Utility\ServiceLocator\ServiceLocator;

class Module {

	public function onBootstrap(MvcEvent $e) {
		$sm = $e->getApplication()->getServiceManager();
		ServiceLocator::setServiceLocator($sm);
		$sm->setService('Utility\Log\Logger', $sm->get('Zend\Log\Logger'));
		$session = $sm->get('Zend\Session\SessionManager');
		$session->start();
	}

	public function getAutoloaderConfig() {
		return array(
		    'Zend\Loader\StandardAutoloader' => array(
			'namespaces' => array(
			    __NAMESPACE__ => __DIR__ . '/src/',
			),
		    ),
		);
	}

	public function getServiceConfig() {
		return array(
		    'factories' => array(
			'Zend\Log\Logger' => 'Zend\Log\LoggerServiceFactory',
			'Zend\Session\SessionManager' => function ($sm) {
				$config = $sm->get('config');
				if (isset($config['session'])) {
					$session = $config['session'];

					$sessionConfig = null;
					if (isset($session['config'])) {
						$class = isset($session['config']['class']) ? $session['config']['class'] : 'Zend\Session\Config\SessionConfig';
						$options = isset($session['config']['options']) ? $session['config']['options'] : array();
						$sessionConfig = new $class();
						$sessionConfig->setOptions($options);
					}

					$sessionStorage = null;
					if (isset($session['storage'])) {
						$class = $session['storage'];
						$sessionStorage = new $class();
					}

					$sessionSaveHandler = null;
					if (isset($session['save_handler'])) {
						// class should be fetched from service manager since it will require constructor arguments
						$sessionSaveHandler = $sm->get($session['save_handler']);
					}

					$sessionManager = new SessionManager($sessionConfig, $sessionStorage, $sessionSaveHandler);

					if (isset($session['validators'])) {
						$chain = $sessionManager->getValidatorChain();
						foreach ($session['validators'] as $validator) {
							$validator = new $validator();
							$chain->attach('session.validate', array($validator, 'isValid'));
						}
					}
				} else {
					$sessionManager = new SessionManager();
				}
				Container::setDefaultManager($sessionManager);
				return $sessionManager;
			},
		    ),
		);
	}
}
